package com.ybbase.framework.service.system.impl;

import com.ybbase.framework.base.model.TreeModel;
import com.ybbase.framework.base.service.TreeService;
import com.ybbase.framework.model.po.system.Area;
import com.ybbase.framework.mapper.system.AreaMapper;
import com.ybbase.framework.model.po.system.Permission;
import com.ybbase.framework.model.vo.PermissionTreeVO;
import com.ybbase.framework.service.system.AreaService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 系统区域 服务实现类
 * </p>
 *
 * @author cg
 * @since 2019-11-25
 */
@Service
public class AreaServiceImpl extends ServiceImpl<AreaMapper, Area> implements AreaService {

    @Autowired
    TreeService treeService;

    /**
     * 排序地区列表
     *
     * @param list 地区列表
     * @return List<TreeModel>
     * @author cg
     * @date 2019-12-01 21:46
     * @since cloud2.0
     */
    @Override
    public List<TreeModel> findSortArea(List<Area> list) {
        List<TreeModel> firstList = new ArrayList<>();
        List<TreeModel> sourceList = new ArrayList<>();
        for(Area area:list){
            TreeModel treeModel = assemble(area);
            if(area.getParentId() == 0){
                firstList.add(treeModel);
            }
            sourceList.add(treeModel);
        }
        treeService.sortTree(firstList, sourceList);
        return firstList;
    }

    /**
     * 填充实体
     * @param area 地区实体
     * @return TreeModel 树模型
     * @author cg
     * @date 2019-12-01 22:06
     * @since cloud2.0
     */
    private TreeModel assemble(Area area){
        TreeModel treeModel = new TreeModel();
        treeModel.setId(area.getId().toString());
        treeModel.setPid(area.getParentId().toString());
        treeModel.setName(area.getName());
        treeModel.setValue(area.getId().toString());
        treeModel.setSort(area.getSort());
        Map<String,Object> map = new HashMap<>();
        map.put("code", area.getCode());
        map.put("delFlag", area.getDelFlag());
        treeModel.setExtendsMap(map);
        return treeModel;
    }
}
